package com.lcn.comm.framework.task;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * <p>类说明</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @create: 2017-12-25 13:16
 */
public class TxTask extends Task {

    private Task task;

    public TxTask(Task task) {
        this.task = task;
    }

    public boolean isNotify() {
        return task.isNotify();
    }

    public boolean isAwait() {
        return task.isAwait();
    }

    public String getGroupId() {
        return task.getGroupId();
    }

    public void setGroupId(String groupId) {
        task.groupId = groupId;
    }

    public String getRequestId() {
        return task.requestId;
    }

    public void setRequestId(String requestId) {
        task.requestId = requestId;
    }

    public int getState() {
        return task.state;
    }

    public void setState(int state) {
        task.state = state;
    }

    public void setCallBack(ICallBack callBack) {
        task.callBack = callBack;
    }

    public ICallBack getCallBack() {
        return task.callBack;
    }

    /**
     * 让当前线程处于等待
     */
    public void waitTask() {
        task.waitTask();
    }

    /**
     * 唤醒当前线程
     */
    public void signalTask() {
        task.signalTask();
    }

    /**
     * task是否已删除
     *
     * @return
     */
    public boolean isRemove() {
        return task.isRemove();
    }

    /**
     * 删除任务
     */
    public void remove() {
        task.remove();
        boolean hasData = true;//true没有，false有
        String groupId = getGroupId();
        TaskGroup taskGroup = TaskGroupManager.getInstance().getTaskGroup(groupId);
        if (taskGroup != null) {
            Map<String, TxTask> taskMap = taskGroup.getTaskMap();
            Set<Map.Entry<String, TxTask>> entrySet = taskMap.entrySet();
            Iterator<Map.Entry<String, TxTask>> iter = entrySet.iterator();
            while (iter.hasNext()) {
                Map.Entry<String, TxTask> entry = iter.next();
                if (!entry.getValue().isRemove()) {
                    hasData = false;
                }
            }
            if (hasData) {
                TaskGroupManager.getInstance().removeKey(groupId);
            }
        }
    }
}
